package com.physics.mps.module.file;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;
import com.physics.mps.common.util.AssertUtil;
import com.physics.mps.entity.enums.CodeEnum;
import com.physics.mps.entity.vo.SendFileVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import static com.physics.mps.entity.enums.CodeEnum.*;

/**
 * @author mcsoft
 * @since 2023/8/26
 */
@Slf4j
@Service
public class StpFileService implements FileService {

    // ~ Variable & Constant
    // ========================================================================================================

    @Value("${simulate.model-converter.stp2gltf-path}")
    private String stp2gltfPyPath;

    @Value("${simulate.model-converter.output-path}")
    private String outputPath;


    // ~ Public method
    // ========================================================================================================

    @Override
    public SendFileVo handleFile(SendFileVo sendFileVo) {
        String s = this.stpConvertToGltf(sendFileVo);
        return null;
    }


    // ~ Override method
    // ========================================================================================================


    // ~ Private method
    // ========================================================================================================


    /**
     * 调用 FreeCad 接口，把 STP 文件转为 GLTF
     *
     * @param sendFileVo 发送文件 Vo 对象
     * @return 转换过后的文件路径
     */
    public String stpConvertToGltf(SendFileVo sendFileVo) {
        String stpPath = sendFileVo.getStpPath();

        // 判断stp文件、gltf输出目录是否存在
        AssertUtil.isFalse(CodeEnum.STL_FILE_NOT_EXIST, FileUtil.exist(stpPath));
        AssertUtil.isBlank(outputPath, GLTF_OUTPUT_NOT_EXIST);
        AssertUtil.isFalse(CodeEnum.GLTF_OUTPUT_NOT_EXIST, FileUtil.exist(outputPath));

        // 获取文件名字
        String fileName = FileUtil.getName(stpPath);

        // 生成gltfFile路径, 路径格式: X:\xxx\xxx\gltfFileName-timestamp.gltf
        String gltfPath = outputPath;
        if (!gltfPath.endsWith("\\")) {
            gltfPath += "\\";
        }

        // gltfPath += "gltf\\" + FileUtil.getPrefix(fileName) + "-" + DateUtil.format(new Date(System.currentTimeMillis()), DatePattern.PURE_DATETIME_FORMAT) + ".gltf";
        gltfPath += "gltf\\" + FileUtil.getPrefix(fileName) + ".gltf";

        AssertUtil.isFalse(STP_CONVERT_TO_GLTF_FAILED, FileUtil.exist(stp2gltfPyPath));

        // 执行python程序进行格式转换
        String convertResult = RuntimeUtil.execForStr("python", stp2gltfPyPath, stpPath, gltfPath);

        log.info("STP->GLTF 文件转换执行结果: " + convertResult);
        AssertUtil.isFalse(STP_CONVERT_TO_GLTF_FAILED, FileUtil.exist(gltfPath));
        sendFileVo.setGltfPath(gltfPath);

        return gltfPath;
    }

    // ~ Autowired
    // ========================================================================================================


}
